package com.letou.ls.view;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TableBottmBady extends TableLayout {

	private ArrayList<ArrayList<String>> mDataList;
	// private int cellBorder = 1;
	private int tableTextColor = Color.BLACK;
	private Typeface tableTypeface = Typeface.SANS_SERIF;
	private int tableGravity = Gravity.CENTER;
	private float mLeafTextSize;
	private ArrayList<Float> mWidthList;
	private int iconId;
	private int[] mTypeList;

	public TableBottmBady(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public TableBottmBady(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public TableBottmBady(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs);
	}

	// public void addData(ArrayList<ArrayList<String>> datalist,
	// ArrayList<Float> widthList, float textSize, int icon) {
	// this.setBackgroundColor(Color.parseColor("#3962b4"));
	// this.setBaselineAligned(true);
	//
	// this.removeAllViews();
	// mDataList = datalist;
	// mWidthList = widthList;
	// mLeafTextSize = textSize;
	// iconId = icon;
	// drawTableBody();
	// }

	public void addData(ArrayList<ArrayList<String>> datalist, int[] typeList, ArrayList<Float> widthList,
			float textSize, int icon) {
		this.setBackgroundColor(Color.parseColor("#3962b4"));
		this.setBaselineAligned(true);

		this.removeAllViews();
		mDataList = datalist;
		mWidthList = widthList;
		mLeafTextSize = textSize;
		mTypeList = typeList;
		iconId = icon;
		drawTableBody();
	}

	public void drawTableBody() {
		/**
		 * 画表内容
		 */
		if (mDataList != null) {
			int size = mDataList.size();
			int typeSize = 0;
			if (mTypeList != null) {
				typeSize = mTypeList.length;
			}
			TableLayout tableLayout = new TableLayout(this.getContext());
			TableRow allRow = new TableRow(this.getContext());
			for (int index = 0; index < size; index++) {
				TableRow row = new TableRow(this.getContext());

				row.setPadding(0, 0, 0, 2);
				int ss = mDataList.get(index).size();
				for (int j = 0; j < ss; j++) {

					String label = mDataList.get(index).get(j);
//					TableLayout itemLayout = new TableLayout(this.getContext());
					TableRow itemRow = new TableRow(this.getContext());

					TextView dataCell = new TextView(this.getContext());

					dataCell.setTextColor(tableTextColor);
					dataCell.setBackgroundColor(Color.WHITE);

					dataCell.setTypeface(tableTypeface);
					dataCell.setGravity(tableGravity);
					// int height;
					dataCell.setTextSize(mLeafTextSize);
					dataCell.setText(label);
					if (mWidthList.size() > j) {
						dataCell.setWidth(mWidthList.get(j).intValue() - 1);
					} else {
						// dataCell.setWidth(baseW);
					}

					int type = 0;
					if (typeSize > 0 && index < typeSize) {
						type = mTypeList[j];
					}
					switch (type) {
					case 0:
						// mBgPaint.setColor(Color.WHITE);
						break;
					case 1:
						itemRow.setBackgroundColor(Color.RED);
						break;
					case 2:
						itemRow.setBackgroundColor(Color.parseColor("#FF7F00"));
//						itemRow.setBackgroundColor(Color.YELLOW);
						break;
					}

					TableRow.LayoutParams t = new TableRow.LayoutParams();
					t.setMargins(0, 0, 2, 0);
					// dataCell.setPadding(1, 0, 1, 0);
					dataCell.setLayoutParams(t);
					itemRow.addView(dataCell);
					row.addView(itemRow);
				}
				tableLayout.addView(row);
			}

			ImageView imageView = new ImageView(this.getContext());
			imageView.setScaleType(ScaleType.FIT_XY);
			imageView.setImageResource(iconId);
			imageView.setBackgroundColor(Color.WHITE);
			TableRow.LayoutParams t = new TableRow.LayoutParams();
			// t.span = 2;
			// t.column = 2;
			t.setMargins(1, 0, 1, 0);
			//t.width = mWidthList.get(mWidthList.size() - 1).intValue()*4;
			t.height = TableRow.LayoutParams.MATCH_PARENT;
			imageView.setPadding(1, 0, 1, 0);
			imageView.setLayoutParams(t);
			allRow.addView(tableLayout);
			allRow.addView(imageView);
			addView(allRow);

		}
	}

}
